package com.santiao.fuel.ui.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.databinding.DataBindingUtil;

import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.interfaces.OnSelectListener;
import com.lxj.xpopupext.listener.CityPickerListener;
import com.lxj.xpopupext.popup.CityPickerPopup;
import com.santiao.publics.ui.SettingFragment;
import com.santiao.fuel.R;
import com.santiao.fuel.bean.ladder;
import com.santiao.fuel.databinding.ActivityCalculateBinding;
import com.santiao.fuel.databinding.ActivityWaterCalculateBinding;
import com.santiao.fuel.ui.widget.BaseActivity;
import com.santiao.fuel.utils.PowerPrice;
import com.santiao.fuel.utils.WaterUtils;

import java.text.DecimalFormat;
import java.util.Map;

public class CalculateWaterActivity extends BaseActivity {
    ActivityWaterCalculateBinding binding;
    ladder price;
    double dl;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_water_calculate);
        binding= DataBindingUtil.setContentView(this,R.layout.activity_water_calculate);
        setToolbar(R.id.toolbar_power,R.id.title_power,"水费价格");
        Map<String, ladder> city = WaterUtils.getCity();
        price=city.get("上海市");
        String[] cityNames = city.keySet().toArray(new String[0]);
        binding.constraintLayout2.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                new XPopup.Builder(CalculateWaterActivity.this)
                        .maxHeight(1000)
                        .asBottomList("请选择城市", cityNames,
                                new OnSelectListener() {
                                    @Override
                                    public void onSelect(int position, String text) {
                                        binding.textView2.setText(text);
                                        price=city.get(text);
                                        if (binding.edit.getText().toString().isEmpty())
                                            add(0);
                                        else {
                                            add( Double.parseDouble(binding.edit.getText().toString()));
                                        }

                                    }
                                })
                        .show();
            }
        });
        binding.edit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!s.toString().isEmpty()){
                    dl=Double.parseDouble(s.toString());
                    add(dl);
                }else {
                    add(0.0);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });


        SettingFragment.adLoad(1,null,null,this,this);
    }

    private void add(double dl) {
        double sum = 0.0;  // 总费用
        DecimalFormat df = new DecimalFormat("#0.00");  // 格式化输出

        if (dl <= 22) {  // 第一阶梯
            sum = dl * price.getFirst();
        } else if (dl > 22 && dl <= 30) {  // 第二阶梯
            sum = 22 * price.getFirst();  // 第一阶梯费用
            sum += (dl - 22) * price.getSecond();  // 第二阶梯费用
        } else {  // 第三阶梯
            sum = 22 * price.getFirst();  // 第一阶梯费用
            sum += 8 * price.getSecond();  // 第二阶梯费用 (22-30之间)
            sum += (dl - 30) * price.getThird();  // 第三阶梯费用
        }

        binding.sum.setText(df.format(sum));  // 显示总费用
    }

}